#include "log_save.h"
#include <QFile>
#include <QIODevice>
#include <QDebug>
#include <QDateTime>


Log_save::Log_save(QObject *parent,QString FilePath) : QObject(parent)
{
    //打开一个文件
    uint8_t Head[2] = {0xAA,0xBB};
    QByteArray read_data;
    QFile save_info(FilePath);

    File_path = FilePath;

    connect(this,&Log_save::Update_log_signal,this,&Log_save::Save_log_cb);

    if(!save_info.open(QIODevice::ReadWrite)){
        return;
    }//以写的方式打开文件，如果文件不存在则创建

    read_data = save_info.read(save_info.size());

    if(read_data[0].operator!=(0xAA)){
        qDebug()<<"clear_info";
        read_data.clear();
        save_info.seek(0);
        save_info.write(read_data);
        save_info.write((char *)Head,sizeof (Head));
    }
    save_info.close();
}

QString change_uint8_to_String(uint8_t * data)
{

    QString hexString = QString::asprintf("%02X", static_cast<unsigned char>(data[0]));
    for (int i = 1; i < 6; i++) {
        hexString += QString::asprintf("%02X", static_cast<unsigned char>(data[i]));
    }
    return hexString;
}

void Log_save:: Save_log_cb(QString log)
{
    QFile save_info(File_path);

    if(!save_info.open(QIODevice::Append  | QIODevice::Text))
    {
        return ;
    }//以写的方式打开文件，如果文件不存在则创建

    QTextStream out(&save_info);

    QDateTime CurrentTime = QDateTime::currentDateTime();
    QByteArray time  = "[";
    time.append(CurrentTime.toString("HH:mm:ss"));
    time.append("]");
    time.append(log);
    time.append("\r\n");

    out<<time;

//    save_info.seek(save_info.size()); //移动到末尾
//    save_info.write(time); //写入数据

    save_info.close();
    qDebug()<<"save:"<<time;
}

uint8_t Log_save::write_log_info_toFile(QString log)
{

    QString buf;
    QString h_mac;
    QByteArray read_data;
    QFile save_info(File_path);

    if(!save_info.open(QIODevice::ReadWrite))
    {
        return 0;
    }//以写的方式打开文件，如果文件不存在则创建

//    info.head = '<';
//    info.tail = '>';
//    if(info.mac[0] == 0x00){
//        memset(info.mac,0xFF,6);
//    }
//    h_mac = change_uint8_to_String(info.mac);

//    read_data.clear();
//    read_data.setRawData((char *)&info,sizeof (info)); //拷贝内容到数组
//    buf.append('<');
//    buf.append(QString::number(info.identifier));
//    buf.append(':');
//    buf.append(h_mac);
//    buf.append('>');
//    buf.append('\n');

    save_info.seek(save_info.size()); //移动到末尾
    save_info.write(buf.toUtf8()); //写入数据

    save_info.close();

    return 0;
}

QByteArray get_used_info(void)
{
    QByteArray read_data;
    QFile save_info("info.txt");

    if(!save_info.open(QIODevice::ReadOnly)){
        return 0;
    }//以写的方式打开文件，如果文件不存在则创建

    read_data.clear();
    read_data = save_info.read(save_info.size());

    save_info.close();

    return read_data;
}

uint8_t Duplicate_check(int id)
{
    QByteArray read_data;
    QFile save_info("info.txt");
    QByteArray find_format;

    if(!save_info.open(QIODevice::ReadOnly)){
        return 0;
    }//以写的方式打开文件，如果文件不存在则创建

    read_data.clear();
    read_data = save_info.read(save_info.size());
    save_info.close();

    find_format.append('<');
    find_format.append(QByteArray::number(id));
    find_format.append(":");
//    qDebug()<<"find:"<<find_format;

    if(read_data.count(find_format) > 0){
        return 0;
    }

    return 1;
}

void Log_save::load_last_info_to_lineEdit(void)
{
    QString s_id;
    QString s_mac;

    int number;
    int index_left = 0,index_right = 0,index_end = 0;

    QByteArray data =  get_used_info();
    number = data.count(':');
    if(number < 1){
        return;
    }

    index_right = data.size();
    //    qDebug()<<"已用个数:"<<number<<"size:"<<data.size();

    index_left = data.lastIndexOf('<',index_right);
    index_right = data.indexOf(":",index_left);
    index_end = data.indexOf('>',index_right);

    if((index_left == -1)||(index_right == -1)||(index_end == -1)){return;}

    //    qDebug()<<index_left<<index_right<<"取出end:"<<index_end;
    //取出编号
    s_id =  data.mid(index_left+1,index_right-index_left-1);
    s_mac =  data.mid(index_right+1,index_end-index_right-1);
    //s_mac = change_byte_to_String(mac);

    //    qDebug()<<"取出id:"<<s_id;
    //    qDebug()<<"取出mac:"<<s_mac;
//    ui->lineEdit_name_identifier->setText(s_id);
//    ui->lineEdit_mac_address->setText(s_mac);

}

